# 项目名称
PROJECT_NAME := pceservice
# 版本
VERSION := 0.1.0
# 构建日期
BUILD_DATE := $(shell date +%Y%m%d)
# 构建哈希
BUILD_HASH := $(shell git rev-parse --short HEAD)

# 二进制输出目录
BIN_DIR := bin
# 可执行文件名称
BINARY := $(BIN_DIR)/$(PROJECT_NAME)

# Go 命令
GO := go
# Go 构建命令
GO_BUILD := $(GO) build
# Go 测试命令
GO_TEST := $(GO) test
# Go 依赖下载命令
GO_GET := $(GO) get
# Go 格式化命令
GO_FMT := $(GO) fmt
# Go 代码检查命令
GO_LINT := $(GO) vet

# 所有包
PACKAGES := $(shell $(GO) list ./... | grep -v /vendor/)

# 默认目标
all: build

# 构建项目
build: clean fmt lint
	$(GO_BUILD) -o $(BINARY) -ldflags "-X main.Version=$(VERSION) -X main.BuildDate=$(BUILD_DATE) -X main.BuildHash=$(BUILD_HASH)" ./cmd/pceservice

# 运行测试
test:
	$(GO_TEST) -v $(PACKAGES)

# 格式化代码
fmt:
	$(GO_FMT) $(PACKAGES)

# 代码检查
lint:
	$(GO_LINT) $(PACKAGES)

# 清理构建文件
clean:
	rm -rf $(BIN_DIR)

# 运行项目
run: build
	$(BINARY) -config config/config.yaml

# 构建Docker镜像
docker-build:
	docker build -t $(PROJECT_NAME):$(VERSION) .

# 运行Docker容器
docker-run:
	docker run -p 8080:8080 -p 4189:4189 --name $(PROJECT_NAME) $(PROJECT_NAME):$(VERSION)

# 构建并推送Docker镜像
docker-push: docker-build
	docker tag $(PROJECT_NAME):$(VERSION) $(PROJECT_NAME):latest
	docker push $(PROJECT_NAME):$(VERSION)
	docker push $(PROJECT_NAME):latest

# 帮助信息
help:
	@echo "可用目标:"
	@echo "  build      - 构建项目"
	@echo "  test       - 运行测试"
	@echo "  fmt        - 格式化代码"
	@echo "  lint       - 代码检查"
	@echo "  clean      - 清理构建文件"
	@echo "  run        - 运行项目"
	@echo "  docker-build - 构建Docker镜像"
	@echo "  docker-run   - 运行Docker容器"
	@echo "  docker-push  - 构建并推送Docker镜像"
	@echo "  help       - 显示此帮助信息"

.PHONY: all build test fmt lint clean run docker-build docker-run docker-push help    